<?php
 function itx_thumbnail($featured=false){
    global $wp_query;
    extract(itx_get_option('front'));
    if($featured){
        $x=$featuredx;
        $y=$featuredy;
    }else{
        $x=$thumbx;
        $y=$thumby;
    }
	
	$img_id=$src='';
    // get post thumbnail (WordPress 2.9+)
    if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
        $img_id=get_post_thumbnail_id();

    //get the thumbnail from custom field
    }elseif($post_img=get_post_meta(get_the_ID(), 'thumb', true)) {
        if (is_numeric($post_img)) $img_id=$post_img;
        else $imgurl=$post_img;
    
    // get one of thumbnail from the post (if any)
    }elseif($catch){
        if ( $images = get_children( 'post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image') ) {
            asort($images);
            $keys=array_keys($images);
            $img_id=$keys[0];
        }
    }
	if ($img_id) {
		$img = wp_get_attachment_image_src( $img_id,'post-thumbnail');

		if ($img[1]!=$x && $img[2]!=$y) $img = wp_get_attachment_image_src($img_id);

		if ($featured){
			$img = wp_get_attachment_image_src( $img_id,'featured');
			if ($img[1]!=$x && $img[2]!=$y){
				$imgl= wp_get_attachment_image_src( $img_id,'medium');
				if ($x>0.9*$imgl[1]||$y>0.9*$imgl[2]){$img=$imgl;$imgl= wp_get_attachment_image_src( $img_id,'large' );}
				if ($x>0.9*$imgl[1]||$y>0.9*$imgl[2]){$img=$imgl;$imgl= wp_get_attachment_image_src( $img_id,'full' );}
				if ($x>0.9*$imgl[1]||$y>0.9*$imgl[2]){$img=$imgl;}
			}
		}

		if ($img){
			$src=$img[0];
			$width=$img[1];
			$height=$img[2];
			$yy=@($x*$height/$width);
			$xx=@($y*$width/$height);
			$ml=$mt='';
			if ($x<$xx){$ml='margin-left: -'.(($xx-$x)/2).'px';}
			elseif ($y<$yy){$mt='margin-top: -'.(($yy-$y)/2).'px';}
			$style=($ml||$mt)?"style='$mt$ml'":false;
		 }elseif ($imgurl){
			$image=@getimagesize($imgurl);
		if ($image){
			$xx=$image[1]*$x/$y;
			$yy=$image[0]*$y/$x;
			if ($x/$y>$image[0]/$image[1]){$height=$yy;$width=$image[0];}
			else {$width=$xx;$height=$image[1];}
		} else {$width=$x;$height=$y;}
			$src=get_bloginfo('template_directory').'/includes/timthumb.php?src='.$imgurl.'&w='.floor($width).'&h='.floor($height).'&zc=1';
		}
	}

    if ($src){
        $size=($x/$y < @($width/$height))?"height='$y'":"width='$x'";
        $src= "<img src='$src' $style $size alt='".get_the_title()."' />";
    //set image replacement
    } elseif($not==2){
        if ($featured&&$fnotimg) $src= "<img src='$fnotimg' alt='".get_the_title()."'  />";
        elseif(!$featured&&$notimg) $src= "<img src='$notimg' alt='".get_the_title()."' />";
    } elseif($not==1){
		$src= '<img src="'.get_bloginfo('template_url').'/images/spacer.gif" alt="'.get_the_title().'" width="'.$x.'" height="'.$y.'" />';
	} else {return;}
    
    return '<div class="ui-widget-header cont"><a href="'.get_permalink().'">'.$src.'</a></div>';

}

function itx_front_styles(){
    extract(itx_get_option('front'));
    $postwidth=($type=='traditional')?100:100/$column;
    echo"
.posts{width:$postwidth%;}
.posts .meta{width:{$thumbx}px;}
.itx-thumb .cont{width:{$thumbx}px;height:{$thumby}px;}
.featured .meta{width:{$featuredx}px;}
.itx-featured .cont{width:{$featuredx}px;height:{$featuredy}px;}";
}
?>